#include"hr.h"
#include<iostream>
#include <cstdio> // wz add
#include<string>
#include"mysql_util.h"
using namespace std;
#define LEAVE_TIME 10
#define fix_salary 6000
#define min_salary 3000
extern string username;

void hr_interface(){   
    int choose;
    cout << "\n人事管理系统" << endl;
    cout << "1.员工入职" << endl;
    cout << "2.员工离职" << endl;
    cout << "3.请假安排" << endl;
    cout << "4.补卡申请" << endl;
    cout << "5.退出" << endl;
    cin >> choose;
    switch(choose){
        case 1:
            add_staff();break;
        case 2:
            del_staff();break;
        case 3:
            ask_for_leave();break;
        case 4:
            fill_card_application(username);break;// wz add username in ()
        case 5:
            exit(0);break;
        default:printf("\nInvalid input!\n");
    }
}

void emplyee_interface(){   
    int choose;
    cout << "人事管理系统" << endl;
    cout << "1.请假安排" << endl;
    cout << "2.补卡申请" << endl;
    cout << "3.退出" << endl;
    cout << "请输入你的选择：" << endl;
    cin >> choose;
    switch(choose){
        case 1:
            //ask_for_leave();
            break;
        case 2:
            fill_card_application(username);
            break;
        case 3:
            exit(0);
            break;
        default:printf("\nInvalid input!\n");
    }
}

void add_staff(){
    struct staff sta;
    char choice;
    do{
        cout << "请依次输入以下信息："  << endl;
        while (true) {
			cout << "\n工号：";
			cin >> sta.id;
			if (!is_num(sta.id)) {
				cout << "请正确输入！" << endl;
				continue;
			}
            break;
        }
        if(queryStaffById(sta.id,sta)){
            cout << "该用户已存在" << endl;
            continue;
        }
        // cout << "\n工号：";cin >> sta.id;
        cout << "\n姓名：";cin >> sta.name;
        cout << "\n等级(1~4)：";cin >> (int &)sta.rank;
        cout << "\n年龄：";cin >> sta.age;
        cout << "\n性别(1-男，2-女)：";cin >> (int &)sta.sex; 
        cout << "\n电话：";cin >> sta.tel;
        cout << "\n邮箱：";cin >> sta.mail;
        sta.is_deleted=false;
        bool is_insert=insertStaff(sta);
        if(!is_insert){
            cout << "添加失败" << endl;
            continue;
        }
        cout << "是否继续添加（y/n）?: ";
        cin >> choice;
    }while(choice == 'y');

}
//先备份后删除
void del_staff(){
    string id;
    cout << "请输入要删除的职工号："  << endl;
    cin >> id;
    
    bool ret=deleteStaffById(id);
    if(!ret){
        cout <<"error"<< endl;
    }else{
        cout << "该员工信息已删除" << endl;
    }    
}

//请假
void ask_for_leave(){
    int choose;
    string id;
    struct staff sta;
    while(true){
        cout << "输入学号：" << endl;
        cin >> id;
        //通过id检索数据库看存不存在
        if(!queryStaffById(id,sta)){
            cout << "该用户不存在！" << endl;
			continue;
        }
        break;
    }
    cout << "请假类型" << endl;
    cout << "1.事假" << endl;
    cout << "2.病假" << endl;
    cout << "3.年假" << endl;
    cout << "4.婚丧假" << endl;
    cout << "5.产假（女士）" << endl;
    cout << "6.陪产假（男士）" << endl;
    cin >> choose;
    switch(choose){
        case 1: 
            personal_leave(id);break;
        case 2:
            sick_leave(id);break;
        case 3:
            annual_vacation(id);break;
        case 4:
            marriage_or_funeral_leave(id);break;
        case 5:
            maternity_leave(id);break;
        case 6:
            paternity_leave(id);break;
        default: ask_for_leave();
    }
}
//事假
void personal_leave(string id){
    int day;
    say salary; 
    char ret[20];  
    float deduct;
    strcpy(salary.unsy.id,id.c_str());
    // strcpy(salary.unsy.ymd,"1999-3-1");
    cout << "\n你想请几天事假？" << endl;
    cin >> day;
    getNow(DATE, ret);
    strcpy(salary.unsy.ymd,ret);
    //salary.unsy.total=salary.unsy.total-(salary.unsy.total/30)*day;}
    deduct=(fix_salary/30.0)*day;
    //更新工资（id,deduct）
    if(!sql_deduct_fixed(id,deduct)){
        cout <<"更新工资失败"<< endl;
    }else
    cout << "\n请假成功" << endl;
}

void sick_leave(string id){
    say salary;
    int day;
    char ret[20]; 
    float deduct; 
    strcpy(salary.unsy.id,id.c_str());
    // strcpy(salary.unsy.ymd,"1999-3-1");
    float minsalary=300;//最低工资一个月9000
    cout << "\n你想请几天病假？" << endl;
    cin >> day;
    getNow(DATE, ret);
    strcpy(salary.unsy.ymd,ret);
    deduct=(min_salary/30.0-300)*day;
    //更新工资(id,salary)
    if(!sql_deduct_fixed(id,deduct)){
        cout <<"更新工资失败"<< endl;
    }else
    cout << "\n请假成功" << endl;
}



bool is_num(string id) {
		for (size_t i = 0; i < id.size(); ++i) {
			if (id[i] < '0' || id[i] > '9' ) return false;
		}
		return true;
}

//年假婚假产假陪产假
void annual_vacation(string id)
{
    float day_salary;
    day_salary = fix_salary/30*LEAVE_TIME;
    sql_deduct_fixed(id,day_salary);
    printf("请假成功！您的请假时间为%d天\n",LEAVE_TIME);
}

void marriage_or_funeral_leave(string id)
{
    float day_salary;
    day_salary = fix_salary/30*LEAVE_TIME;
    sql_deduct_fixed(id,day_salary);
    printf("请假成功！您的请假时间为%d天\n",LEAVE_TIME);
}

void maternity_leave(string id)
{
    int day;
    printf("请选择请假天数：\n");
    printf("1.顺产（10天）\n2.剖腹产（15天）\n3.流产（20天）\n");
    scanf("%d",&day);

    float day_salary1,day_salary2,day_salary3;

    if(day == 1)
    {
        day_salary1 = fix_salary/30*LEAVE_TIME;
        sql_deduct_fixed(id,day_salary1);
        printf("请假成功！您的请假时间为%d天\n",LEAVE_TIME);
    }
    if(day == 2)
    {
        day_salary2 = (fix_salary/30)*(LEAVE_TIME+5);
        sql_deduct_fixed(id,day_salary2);
        printf("请假成功！您的fix_salary请假时间为%d天\n",LEAVE_TIME+5);
    }
    if(day == 3)
    {
        day_salary3 = (fix_salary/30)*(LEAVE_TIME+10);
        sql_deduct_fixed(id,day_salary3);
        printf("请假成功！您的请假时间为%d天\n",LEAVE_TIME+10);
    }
}

void paternity_leave(string id)
{
    int day;
    printf("您需要请几天假？\n");
    scanf("%d",&day);

    float day_salary = fix_salary/30*day;

    sql_deduct_fixed(id,day_salary);
    printf("请假成功！您的请假时间为%d天\n",day);
}

//补卡申请
void fill_card_application(string id)
{
    //调用该人员考勤信息，看是否异常，异常可以申请最多4次的补卡，正常不可以
    vector<card_info> v;
    int n = 0;
    char date[10];
    char time[10];
    int tmp = queryAbnormalCardInfo(id, v);
    
    if(tmp == -1){   //系统异常
        printf("系统异常！请联系管理员维护！");
    } 
    else if(tmp != 0){
        //输出打卡异常的日期和时间点
        cout << "以下是您本月的异常打卡信息：" << endl;
        for (vector<card_info>::iterator it = v.begin(); it != v.end(); it++) {
            cout << (*it).today << "\t"  << (*it).punch_in_time << "\t" << (*it).punch_out_time <<endl;
	    }
        cout << endl;
        while(n < 4 && tmp >= 4){
            printf("请输入补卡的日期：");
            string today;
            cin>>today;
            struct card_info ci = {id,today, today + " 07:00:00", today + " 18:00:00",0,0,0,0,660};
            if(updateCardInfo(ci)){
                cout<<"更新打卡信息成功!"<<endl;
            }
            else{
                cout<<"更新打卡信息失败!"<<endl;
            }
            ++n;
            if(n == 4){
                cout<<"抱歉！您的补卡次数已经用完了"<<endl;
            }
        }
        if(tmp < 4){
            while(n < tmp){
                printf("请输入补卡的日期：");
                string today;
                cin>>today;
                struct card_info ci = {id, today, today +" 07:00:00", today +" 18:00:00",0,0,0,0,660};
                if(updateCardInfo(ci)){
                    cout<<"更新打卡信息成功!"<<endl;
                }
                else{
                    cout<<"更新打卡信息失败!"<<endl;
                }
                ++n;
            }
        }
    }
    else{
        printf("您的考勤信息正常，无需补卡！");
    }
}